# MoviesTab - MoviesTab is group of objects that are displayed on movie tab
# Copyright (C) 2007 Lauri Taimila
# 
# Entertainer is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Entertainer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2008, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import gtk
import pango
import clutter

from frontend.user_event import UserEvent
from frontend.gui.screens.screen import Screen
from frontend.gui.transitions.transition import Transition

from frontend.gui.tabs.tab import Tab
from frontend.gui.widgets.image_menu import ImageMenu
from frontend.gui.widgets.list_indicator import ListIndicator
from frontend.gui.widgets.image_menu_item import ImageMenuItem
from frontend.gui.widgets.rounded_texture import RoundedTexture

class MoviesTab(Tab):
    """
    Tab can be used as part of the TabGroup
    
    Tab is a very simple container that contains all the widgets and logic
    of the tab page.
    """
    
    def __init__(self, title, frontend):
        """
        Initialize tab
        @param title: Title of the Tab (string)
        @param frontend: FrontendClient object
        """
        Tab.__init__(self, title)
        self.library = frontend.get_video_library()
        self.frontend = frontend
        self.ui = frontend.getUserInterface()
        self.theme = frontend.getTheme()
        
        if self.library.get_number_of_movies() == 0:
           self._create_empty_library_notice()
        else:
            self._create_movie_menu()
    
    def can_activate(self):
        """
        Allow if we have some movies indexed.
        """
        if self.library.get_number_of_movies() == 0:
            return False
        else:
            return True
            
    def set_active(self, boolean):
        """
        Override method from parent class.
        @param boolean: True to set this tab active, False to set inactive
        """
        Tab.set_active(self, boolean)
        if boolean:
            self._update_movie_info(self.movie_menu.get_current_menuitem().get_userdata())
            self.movie_menu.set_active(True)
            self.movie_title.show()
            self.movie_info.show()
            self.movie_plot.show()
            self.list_indicator.show()
        else:
            self.movie_menu.set_active(False)
            self.movie_title.hide()
            self.movie_info.hide()
            self.movie_plot.hide()
            self.list_indicator.hide()
            
    def _create_empty_library_notice(self):
        """
        Create an information box that is displayed if there are no indexed
        movies.
        """
        Tab.show_empty_tab_notice(self, "No movies available!",
                                  "There are no indexed movies in Entertainer media library. To add movies, click on 'content' button on toolbar and open 'videos' tab. Now click on 'add' button and select some folders which contains movie files.")
    
    def _create_movie_menu(self):
        """
        Create a view that is displayed when there is indexed movies in
        the video library.
        """
        #Create movie menu
        self.movie_menu = ImageMenu(self.theme)
        self.movie_menu.set_item_size(self.ui.x(0.1), self.ui.y(0.25))
        self.movie_menu.set_item_spacing(self.ui.y(0.025))
        self.movie_menu.set_visible_column_count(2)
        self.movie_menu.set_row_count(7)
        self.movie_menu.set_orientation(self.movie_menu.VERTICAL)
        
        # Default texture
        default_cover = clutter.Texture()
        buffer = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("default_movie_art"))
        default_cover.set_pixbuf(buffer)
        
        movies = self.library.get_movies()
        for movie in movies:
            if movie.has_cover_art():
                #texture = clutter.Texture()
                buffer = gtk.gdk.pixbuf_new_from_file(movie.get_cover_art_url())
                #texture.set_pixbuf(buffer)
                texture = RoundedTexture(buffer, self.ui.x(0.1), self.ui.y(0.25))
            else:
                texture = clutter.CloneTexture(default_cover)
            item = ImageMenuItem(self.ui.x(0.1), self.ui.y(0.25), texture)
            item.set_userdata(movie)
            self.movie_menu.add_actor(item)
        
        self.movie_menu.set_position(self.ui.x(0.11), self.ui.y(0.2))
        self.movie_menu.set_active(False)
        self.movie_menu.show()
        
        if self.frontend.get_configuration().show_effects():
            self.movie_menu.set_animate(True)
        
        self.add(self.movie_menu)
        
        # Create list indicator
        self.list_indicator = ListIndicator(self.ui.x(0.13), self.ui.y(0.04), self.theme, ListIndicator.VERTICAL)
        self.list_indicator.set_position(self.ui.x(0.75), self.ui.y(0.76))
        self.list_indicator.set_delimiter(" | ")
        self.list_indicator.set_maximum(self.movie_menu.get_number_of_items())
        self.list_indicator.set_current(1)
        self.add(self.list_indicator)
        
        # Create information labels
        self.movie_title = clutter.Label()
        self.movie_title.set_text(" ")
        self.movie_title.set_font_name(self.theme.getFont() + " bold " + str(self.ui.y(0.042)) + "px")
        self.movie_title.set_color(self.theme.getColor("title"))
        self.movie_title.set_line_wrap(False)
        self.movie_title.set_ellipsize(pango.ELLIPSIZE_END)
        self.movie_title.set_position(self.ui.x(0.2), self.ui.y(0.75))
        self.movie_title.set_width(self.ui.x(0.5))
        self.movie_title.show()
        self.add(self.movie_title)
        
        self.movie_info = clutter.Label()
        self.movie_info.set_text(" ")
        self.movie_info.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.034)) + "px")
        self.movie_info.set_color(self.theme.getColor("subtitle"))
        self.movie_info.set_line_wrap(False)
        self.movie_info.set_ellipsize(pango.ELLIPSIZE_END)
        self.movie_info.set_position(self.ui.x(0.2), self.ui.y(0.8))
        self.movie_info.set_width(self.ui.x(0.5))
        self.movie_info.show()
        self.add(self.movie_info)
        
        self.movie_plot = clutter.Label()
        self.movie_plot.set_name("mi_plot")
        self.movie_plot.set_text("")
        self.movie_plot.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.025)) + "px")
        self.movie_plot.set_color(self.theme.getColor("subtitle"))
        self.movie_plot.set_position(self.ui.x(0.2), self.ui.y(0.85))
        self.movie_plot.set_line_wrap_mode(pango.WRAP_WORD)
        self.movie_plot.set_line_wrap(True)
        self.movie_plot.show()
        self.add(self.movie_plot)

    def _update_movie_info(self, movie):
        """
        Update movie information. Updates movie specific labels.
        @param movie: Currently selected movie (Movie object)
        """
        genres = movie.get_genres()
        if len(genres) > 1:
            genre = genres[0] + "/" + genres[1]
        else:
            genre = genres[0]
            
        self.movie_title.set_text(movie.get_title() + " (" + str(movie.get_year()) + ")")
        self.movie_info.set_text(str(movie.get_runtime()) + "min, " + genre)
        self.movie_plot.set_text(movie.get_short_plot())
        self.movie_plot.set_line_wrap(True)
        self.movie_plot.set_width(self.ui.x(0.7))     

    def handle_user_event(self, event):
        """ 
        Handle screen specific user events
        @param event: UserEvent object
        @return: Boolean, Should control be moved back to tab bar from tab
        """
        type = event.get_type()
        
        if type == UserEvent.NAVIGATE_UP:
            if self.movie_menu.get_current_postition() <= 6:
                return True # Move control back to tab bar
            else:
                self.movie_menu.move(self.movie_menu.UP)
                self._update_movie_info(self.movie_menu.get_current_menuitem().get_userdata())
                self.list_indicator.set_current(self.movie_menu.get_current_postition() + 1)
        
        elif type == UserEvent.NAVIGATE_DOWN:
            self.movie_menu.move(self.movie_menu.DOWN)
            self._update_movie_info(self.movie_menu.get_current_menuitem().get_userdata())
            self.list_indicator.set_current(self.movie_menu.get_current_postition() + 1)
        
        elif type == UserEvent.NAVIGATE_LEFT:
            self.movie_menu.move(self.movie_menu.LEFT)
            self._update_movie_info(self.movie_menu.get_current_menuitem().get_userdata())
            self.list_indicator.set_current(self.movie_menu.get_current_postition() + 1)
            
        elif type == UserEvent.NAVIGATE_RIGHT:
            self.movie_menu.move(self.movie_menu.RIGHT)
            self._update_movie_info(self.movie_menu.get_current_menuitem().get_userdata())
            self.list_indicator.set_current(self.movie_menu.get_current_postition() + 1)
        
        elif type == UserEvent.NAVIGATE_SELECT:
            movie = self.movie_menu.get_current_menuitem().get_userdata()
            self.ui.changeScreen(self.ui.createScreen("movie", movie), Transition.FORWARD)
            
        return False
    